﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAKS.Models;
using NHibernate;
using NHibernate.Criterion;

namespace SAKS.Services.Query
{
    public enum SortColumn
    {
        CreatedDateAsc,
        CreatedDateDesc,
        Name
    }

    class PagedProjectBriefSearch
        : PagedQueryOverBase<ProjectBrief>
    {
        public PagedProjectBriefSearch(ISession session):base(session){}
        public string Name { get; set; }
        public SortColumn Sort { get; set; }

        protected override IQueryOver<ProjectBrief, ProjectBrief> GetQuery()
        {
            var query = Session.QueryOver<ProjectBrief>();
            if (!string.IsNullOrEmpty(Name))
                query = query.WhereRestrictionOn(p => p.Name)
                  .IsInsensitiveLike(Name, MatchMode.Anywhere);

            switch (Sort)
            {
                case SortColumn.CreatedDateAsc:
                    query = query.OrderBy(p => p.CreatedDate).Asc;
                    break;
                case SortColumn.Name:
                    query = query.OrderBy(p => p.Name).Asc;                   
                    break;
                default:
                    query = query.OrderBy(p => p.CreatedDate).Desc;
                    break;
            }
            return query;
        }
    }
}
